package com.cms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.constant.UserCoinConstant;
import com.cms.service.ReportService;
import com.cms.service.UserCoinLogService;
import com.google.gson.Gson;

@Controller
public class CashController extends BaseController{
    
     @Autowired
	 private UserCoinLogService userCoinLogService;
     
     @Autowired
     private ReportService reportService;
 
	 @RequestMapping(value="/userCash/getUserCashInfo.do")
	 public String getUserCashInfo(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {
		 map.put("userCash", this.reportService.querySingleResult("getUserCashInfo", this.buildQueryMap(req)));
		 return "cash/userCash.html";
	 }
	 
	 
	 @RequestMapping(value="/userCash/handlerUserCash.do")
	 public void handlerUserCash(HttpServletRequest req,HttpServletResponse resp,ModelMap map)throws Exception
	 {  
	    int status =StringUtils.str2Int(req.getParameter("status"));
	    Map<String,Object>out = new HashMap<String, Object>();
	    Map<String,Object>result = this.reportService.querySingleResult("getUserCashInfo", this.buildQueryMap(req));
	    String cashStatus = result.get("status")+"";
	    if(result!=null&&(StringUtils.equals(UserCoinConstant.WITHDRAWAL_WAIT, cashStatus) 
	    		||StringUtils.equals(UserCoinConstant.WITHDRAWAL_NO, cashStatus)))
	    {
	    	try {
			    this.userCoinLogService.handlerUserCash(status, req.getParameter("remark"),result);
			    out.put(CmsConstant.STATUS, CmsConstant.SUCCESS_STATUS);
			} catch (Exception e) {
				out.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
				out.put(CmsConstant.MSG, e.getMessage());
			}
			this.renderText(resp, new Gson().toJson(out));
			return;
	    }
	    out.put(CmsConstant.STATUS, CmsConstant.FAIL_STATUS);
    	out.put(CmsConstant.MSG, "该提现请求已处理，请刷新页面重试。");
    	this.renderText(resp, new Gson().toJson(out));
	 }
	  
}
